﻿namespace HiMap.MapControls
{
    using HiMap.MapControls.Tools;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class ToolboxControl : UserControl
    {
        private bool _Collapse;
        private MapControl _Map;
        private IContainer components;
        private int leftIndex = -1;
        private List<MeTool> listTool;
        private Panel panMain;
        private PictureBox picLeft;
        private PictureBox picRight;
        private PictureBox picSelected;
        private int PicShowCount = -1;
        private PictureBox picVisible;
        private int rightIndex = -1;
        private int width;

        public ToolboxControl()
        {
            this.InitializeComponent();
            this.listTool = new List<MeTool>();
            this.picSelected = null;
            this._Collapse = false;
            this.width = 0;
        }

        public void AddTool(ICommand Cmd, string Caption, Image Img, Image SelectedImg)
        {
            MeTool tool = new MeTool();
            tool.Cmd = Cmd;
            tool.Caption = Caption;
            tool.Img = Img;
            tool.SelectedImg = SelectedImg;
            tool.Type = "Command";
            this.listTool.Add(tool);
        }

        public void AddTool(ITool Cmd, string Caption, Image Img, Image SelectedImg)
        {
            MeTool tool = new MeTool();
            tool.Cmd = Cmd;
            tool.Caption = Caption;
            tool.Img = Img;
            tool.SelectedImg = SelectedImg;
            tool.Type = "Tool";
            this.listTool.Add(tool);
        }

        private void CreatePic(MeTool tl, int Index)
        {
            if (this.PicShowCount == -1)
            {
                double num = this.panMain.Width / 30;
                this.PicShowCount = Convert.ToInt32(num.ToString().Split(new char[] { '.' })[0]);
            }
            PictureBox box = new PictureBox();
            box.Name = "pic" + Index.ToString();
            box.Dock = DockStyle.Left;
            if (this.PicShowCount > Index)
            {
                box.Width = 30;
                if (this.rightIndex == -1)
                {
                    this.rightIndex = Index + 1;
                }
            }
            else
            {
                box.Width = 0;
            }
            box.SizeMode = PictureBoxSizeMode.StretchImage;
            box.Visible = true;
            box.Click += new EventHandler(this.PicClick);
            box.Image = tl.Img;
            this.panMain.Controls.Add(box);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(ToolboxControl));
            this.picLeft = new PictureBox();
            this.picRight = new PictureBox();
            this.picVisible = new PictureBox();
            this.panMain = new Panel();
            base.SuspendLayout();
            this.picLeft.BackColor = Color.LightGray;
            this.picLeft.Dock = DockStyle.Left;
            this.picLeft.Image = (Image) manager.GetObject("picLeft.Image");
            this.picLeft.Location = new Point(0, 0);
            this.picLeft.Name = "picLeft";
            this.picLeft.Size = new Size(0x10, 30);
            this.picLeft.SizeMode = PictureBoxSizeMode.StretchImage;
            this.picLeft.Click += new EventHandler(this.picLeft_Click);
            this.picRight.BackColor = Color.LightGray;
            this.picRight.Dock = DockStyle.Right;
            this.picRight.Image = (Image) manager.GetObject("picRight.Image");
            this.picRight.Location = new Point(0x10, 0);
            this.picRight.Name = "picRight";
            this.picRight.Size = new Size(0x10, 30);
            this.picRight.SizeMode = PictureBoxSizeMode.StretchImage;
            this.picRight.Click += new EventHandler(this.picRight_Click);
            this.picVisible.BackColor = Color.LightGray;
            this.picVisible.Dock = DockStyle.Right;
            this.picVisible.Image = (Image) manager.GetObject("picVisible.Image");
            this.picVisible.Location = new Point(0x20, 0);
            this.picVisible.Name = "picVisible";
            this.picVisible.Size = new Size(0x10, 30);
            this.picVisible.SizeMode = PictureBoxSizeMode.StretchImage;
            this.picVisible.Click += new EventHandler(this.picVisible_Click);
            this.panMain.BackColor = Color.FromArgb(0xc9, 220, 0xe9);
            this.panMain.Dock = DockStyle.Fill;
            this.panMain.Location = new Point(0x10, 0);
            this.panMain.Name = "panMain";
            this.panMain.Size = new Size(0, 30);
            
            base.AutoScaleDimensions = new SizeF(96f, 96f);
            base.AutoScaleMode = AutoScaleMode.Dpi;
            base.Controls.Add(this.panMain);
            base.Controls.Add(this.picRight);
            base.Controls.Add(this.picVisible);
            base.Controls.Add(this.picLeft);
            base.Name = "ToolboxControl";
            base.Size = new Size(0x30, 30);
            base.ResumeLayout(false);
        }

        private void PicClick(object sender, EventArgs e)
        {
            PictureBox box = (PictureBox) sender;
            MeTool tool = this.listTool[Convert.ToInt32(box.Name.ToString().Substring(3))];
            box.Image = tool.SelectedImg;
            box.Refresh();
            if (tool.Type == "Command")
            {
                ICommand cmd = (ICommand) tool.Cmd;
                cmd.OnCreate(this.Map);
                cmd.RunCommandControl = box;
                cmd.OnClick();
                box.Image = tool.Img;
                box.Refresh();
            }
            else
            {
                if (this.picSelected != null)
                {
                    MeTool tool2 = this.listTool[Convert.ToInt32(this.picSelected.Name.ToString().Substring(3))];
                    this.picSelected.Image = tool2.Img;
                    box.Refresh();
                }
                ITool tool3 = (ITool) tool.Cmd;
                tool3.OnCreate(this.Map);
                this.Map.CurTool = tool3;
                this.picSelected = (PictureBox) sender;
            }
        }

        private void picLeft_Click(object sender, EventArgs e)
        {
            if (!this.Collapse)
            {
                this.Collapse = true;
            }
            else if (this.leftIndex != -1)
            {
                this.panMain.Controls[(this.listTool.Count - 1) - this.leftIndex].Width = 30;
                this.rightIndex = this.leftIndex + this.PicShowCount;
                this.leftIndex--;
                this.panMain.Controls[(this.listTool.Count - 1) - this.rightIndex].Width = 0;
            }
        }

        private void picRight_Click(object sender, EventArgs e)
        {
            if ((this.rightIndex < this.listTool.Count) && (this.rightIndex != -1))
            {
                this.panMain.Controls[(this.listTool.Count - 1) - this.rightIndex].Width = 30;
                this.leftIndex = this.rightIndex - this.PicShowCount;
                this.rightIndex++;
                this.panMain.Controls[(this.listTool.Count - 1) - this.leftIndex].Width = 0;
            }
        }

        private void picVisible_Click(object sender, EventArgs e)
        {
            if (this.Collapse)
            {
                this.Collapse = false;
            }
            else
            {
                this.Collapse = true;
            }
        }

        public void ShowTools()
        {
            for (int i = 0; i < this.listTool.Count; i++)
            {
                this.CreatePic(this.listTool[(this.listTool.Count - 1) - i], (this.listTool.Count - 1) - i);
            }
        }

        public bool Collapse
        {
            get
            {
                return this._Collapse;
            }
            set
            {
                if (this.width == 0)
                {
                    this.width = base.Width;
                }
                this._Collapse = value;
                if (this.Collapse)
                {
                    base.Width = this.width;
                }
                else
                {
                    base.Width = 0x10;
                }
            }
        }

        public MapControl Map
        {
            get
            {
                return this._Map;
            }
            set
            {
                this._Map = value;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct MeTool
        {
            public object Cmd;
            public string Caption;
            public Image Img;
            public Image SelectedImg;
            public string Type;
        }
    }
}

